package com.spring.activityApi.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.fasterxml.jackson.databind.util.JSONPObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mangofactory.swagger.annotations.ApiIgnore;
import com.spring.activityApi.web.service.ActivityApiService;
import com.spring.common.web.service.CommonService;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.dao.JedisCache;


/**
 * Created by wojunyu on 2015/7/26.
 */
@Controller
@RequestMapping(value = "/activityApi")
@ApiIgnore
public class ActivityApiController {
   // static Logger logger = Logger.getLogger(AppQuestController.class.getName());
    @Resource
    private JedisCache jedisCache;
    @Resource
    private ActivityApiService activityApiService;
	@Resource 
	private CommonService commonService;
    
    /*
    @RequestMapping(value = "/getActivityList")
    @ResponseBody
    public Object getActivityList(String token, String flag,String callback) {
    
    	Map<String, Object> chectToken=this.checkToken(token);
    	
    	if(chectToken!=null){
    		
    		if(ApplicationUtils.isNotNull(callback)){
            	return new JSONPObject(callback, chectToken);
            }else{
            	return chectToken;
            }
    	}
    	
    	String c_user_id=jedisCache.lindex(token,0);
    	
    	Map<String,Object> m = new HashMap<String,Object>();
    	
    	try{
    	
    	List<ActivityList> dateset=activityApiService.getActivityList(c_user_id, flag);
    	
    	m.put("status", "1");
        m.put("msg", "取数成功");
        m.put("dataset", dateset);
    	
    	}catch(Exception ex){
    		ex.printStackTrace();
    		m.put("status", "0");
            m.put("msg", "系统报错");
    	}
    	
    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
    }
    
    
    

    @RequestMapping(value = "/getActivityDetail")
    @ResponseBody
    public Object getActivityDetail(String token ,String id,String callback) {
    
    	Map<String, Object> chectToken=this.checkToken(token);
    	
    	if(chectToken!=null){
    		
    		if(ApplicationUtils.isNotNull(callback)){
            	return new JSONPObject(callback, chectToken);
            }else{
            	return chectToken;
            }
    	}
    	
    	String c_user_id=jedisCache.lindex(token,0);
    	
    	Map<String,Object> m = new HashMap<String,Object>();
    	
    	if(id==null){
    		
    		m.put("status", "0");
            m.put("msg", "请输入活动id");
    		
    		
    	}else{
    	
    	
    	try{
    	
    	List<ActivityDetail> dateset=activityApiService.getActivityDetail(c_user_id, id);
    	
    	m.put("status", "1");
        m.put("msg", "取数成功");
        m.put("dataset", dateset);
    	
        
        System.out.println(m.toString());
        
        
    	}catch(Exception ex){
    		ex.printStackTrace();
    		m.put("status", "0");
            m.put("msg", "系统报错");
    	}
    	}
    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
    }
    
    
    
    
    
    
    
    
    
    
    @RequestMapping(value = "/attendActivity")
    @ResponseBody
    public Object attendActivity(String token ,String id,String callback) {
    
    	Map<String, Object> chectToken=this.checkToken(token);
    	
    	if(chectToken!=null){
    		
    		if(ApplicationUtils.isNotNull(callback)){
            	return new JSONPObject(callback, chectToken);
            }else{
            	return chectToken;
            }
    	}
    	
    	String c_user_id=jedisCache.lindex(token,0);
    	
    	Map<String,Object> m = new HashMap<String,Object>();
    	
    	if(id==null){
    		
    		m.put("status", "0");
            m.put("msg", "请输入活动id");
    		
    		
    	}else{
    	
    	
    	try{
    	
    	int state=activityApiService.attendActivity(c_user_id, id);

    	
    	
    	if(state==0){
    		m.put("status", "0");
            m.put("msg", "请输入有效的活动id");
    	}
    	if(state==1){
    		m.put("status", "1");
            m.put("msg", "插入成功");
    	}
    	if(state==2){
    		m.put("status", "1");
            m.put("msg", "更新成功");
    	
    	}
    	
        
        System.out.println(m.toString());
        
        
    	}catch(Exception ex){
    		ex.printStackTrace();
    		m.put("status", "0");
            m.put("msg", "系统报错");
    	}
    	}
    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
    }*/
    
    

    
    @RequestMapping(value = "/getActivityList")
	@ResponseBody
    public Object getActivityList(String activityID,String beginTime, String endTime, 
			String activityTypeCode, String name, String isAppliable, String pageSize, String pageIndex, 
			String skip, String rows,String callback,HttpServletRequest request) {
   
        Map<String, Object> m = new HashMap<String, Object>();
		try {
			
			m  = activityApiService.getActivityList(activityID,beginTime, endTime, activityTypeCode, name, isAppliable, pageSize, pageIndex, skip, rows);
			
			if(activityID!=null){
				commonService.checkIsNewAddr(activityID, "2", request);
			}
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
        
        
    }
			
			
 		
    
    
    @RequestMapping(value = "/appointActivity")
	@ResponseBody
    public Object appointActivity(String token,String cUserName, String activityID,String callback) {
   
        Map<String, Object> m = new HashMap<String, Object>();
		try {

			m  = activityApiService.appointActivity(token,cUserName, activityID);
		
		
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
        
        
    }
    

    
    @RequestMapping(value = "/cancelActivity")
	@ResponseBody
    public Object cancelActivity(String token,String activity_appointmentID,String callback) {
   
        Map<String, Object> m = new HashMap<String, Object>();
		try {

			m  = activityApiService.cancelActivity(token, activity_appointmentID);
		
		
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();

		}

    	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
        
        
    }   
    
    @RequestMapping(value = "/getMyActivityList")
	@ResponseBody
    public Object getMyActivityList(String token, String activityTypeCode, String pageSize,
			String pageIndex, String skip, String rows,String callback) {
   
        Map<String, Object> m = new HashMap<String, Object>();
		try {
			m  = activityApiService.getMyActivityList(token, activityTypeCode, pageSize, pageIndex, skip, rows);
		} catch (Exception e) {
			m.put(ApplicationUtils.STATUS, "0");
			m.put(ApplicationUtils.MSG, "系统异常");
			e.printStackTrace();
		}
   	
    	if(ApplicationUtils.isNotNull(callback)){
        	return new JSONPObject(callback, m);
        }else{
        	return m;
        }
        
        
    }   

    
    
	// 检查token
//	private Map<String, Object> checkToken(String token) {
//		Map<String, Object> s = new HashMap<String, Object>();
//		if (!ApplicationUtils.isNotNull(token)) {
//			s.put(ApplicationUtils.STATUS, "0");
//			s.put(ApplicationUtils.MSG, "请先登录");
//			return s;
//		} else {
//			if (!jedisCache.exists(token)) {
//				s.put(ApplicationUtils.STATUS, "-9999");
//				s.put(ApplicationUtils.MSG, "用户登录已失效，请先重新登录");
//				return s;
//			} else {
//				jedisCache.expire(token, JedisCont.JEDIS_EXPRISE);
//				return null;
//			}
//		}
//	}
}

